<?php
/*
 本软件版权归作者所有,在投入使用之前注意获取许可
 作者：北京市普艾斯科技有限公司
 项目：simcms_锐车1.0
 电话：010-58480317
 Q  Q: 228971357
 网址：http://www.simcms.net
 simcms.net保留全部权力，受相关法律和国际公约保护，请勿非法修改、转载、散播，或用于其他赢利行为，并请勿删除版权声明。
*/
if(!defined('APP_IN')) exit('Access Denied');

// 城市选择
if (!empty($_COOKIE['city'])) {
	$citydata = $db -> row_select_one('area', "id='" . $_COOKIE['city'] . "'", 'parentid');
	$select_province = select_make($citydata['parentid'], $commoncache['provincelist'], "请选择省份");
	$select_city = select_make($_COOKIE['city'], $commoncache['citylist'], "请选择城市");
} else {
	$select_province = select_make('', $commoncache['provincelist'], "请选择省份");
	$select_city = select_make('', $commoncache['citylist'], "请选择城市");
} 
$tpl -> assign('selectprovince', $select_province);
$tpl -> assign('selectcity', $select_city);

//商家类型
$array_dealer_category = arr_dealer_category();
$select_dealer_category = select_make('',$array_dealer_category,"请选择公司类型");
$tpl->assign('times',SMS_CODE_TIME);
$tpl->assign('web_domain',WEB_DOMAIN);
$tpl -> assign('select_dealer_category', $select_dealer_category);

//验证用户名
if (!empty($_POST['param']) and $_POST['name']=="username")
{	
	$data = $db->row_count('member',"username='".$_POST['param']."'");
    if($data==0){
		echo '{"info":"用户名验证成功！","status":"y"}';
	}
	else{
		echo '{"info":"用户名已存在！","status":"n"}';
	}
	exit;
}

//验证邮箱地址
if (!empty($_POST['param']) and $_POST['name']=="email")
{	
	$data = $db->row_count('member',"email='".$_POST['param']."'");
    if($data==0){
		echo '{"info":"邮箱验证成功！","status":"y"}';
	}
	else{
		echo '{"info":"邮箱地址已存在！","status":"n"}';
	}
	exit;
}

//验证手机号
if (!empty($_POST['param']) and $_POST['name']=="mobilephone")
{	
	$data = $db->row_count('member',"mobilephone='".$_POST['param']."'");
    if($data==0){
		echo '{"info":"手机号验证成功！","status":"y"}';
	}
	else{
		echo '{"info":"手机号已存在！","status":"n"}';
	}
	exit;
}

//验证验证码
if (!empty($_POST['param']) and $_POST['name']=="authcode")
{	
    if($_SESSION['authcode'] == $_POST['param']){
		echo '{"info":"验证码正确！","status":"y"}';
	}
	else{
		echo '{"info":"验证码不正确！","status":"n"}';
	}
	exit;
}



//已登陆转向
if (is_user_login()) {
	redirect('',WEB_PATH.'/index.php?mod=user&ac=index');
}

if (submitcheck('mobilephone'))
{
    $arr_not_empty = array('password'=>'密码不能为空','nicname'=>'请填写联系人','mobilephone'=>'请填写手机号','email'=>'电子邮箱不能为空','code'=>'验证码不能为空');
	can_not_be_empty($arr_not_empty, $_POST);
    $_POST['password'] = htmlspecialchars(trim($_POST['password']));
    if ($db->row_count('member',"mobilephone='{$_POST['mobilephone']}'")) showmsg('手机号已存在，请换一个手机号注册',-1);
    if (!is_email($_POST['email'])) showmsg('错误的邮箱格式',-1);
    $code = trim($_POST['code']);

    $record = $db->row_select_one('sms_records', "mobile='" . $_POST['mobilephone'] . "' and type=1", '*', 'id desc');
    if (!$record) showmsg('验证码不存在',-1);
    list($Code, $Time) = explode('_', $record['code']);
    if ($Code != $code)  showmsg('验证码错误',-1);
    if (time() - intval($Time) > SMS_CODE_TIME) showmsg('验证码已过期',-1);


    $post = post('email','mobilephone','password','nicname','company');
    $post['company'] = trim($post['company']);

	$post['mobilephone'] = trim($post['mobilephone']);
	$post['nicname'] = trim($post['nicname']);
	$post['password'] = md5($post['password']);
	$post['regtime'] = TIMESTAMP;
	$post['ischeck'] = 0;
    $post['isdealer'] = 2;

	if($settings['version']==3){
		$post['aid'] = intval($_POST['aid']);
		$post['cid'] = intval($_POST['cid']);
	}

    $rs = $db->row_insert('member',$post);
	$insertid = $db -> insert_id();
    if (!$rs) {
		showmsg('注册失败，请稍后重新尝试',-1);
	}
	else{
        $end_date = MEMBER_VALID_TIME * 2592000 + $post['regtime'];
        $db->row_insert('member_valid', array('member_id' => $insertid, 'start_date' => $post['regtime'], 'end_date' => $end_date));
		$_SESSION['USER_ID'] = $insertid;
		$_SESSION['USER_NAME'] = $_POST['nicname'];
		$rs_user = $db->row_select_one('member',"mobilephone='".trim($_POST['mobilephone'])."'");
		$db->row_update('member',array('last_login_time'=>TIMESTAMP,'last_login_ip'=>get_client_ip(),'login_count'=>$rs_user['login_count']+1),"id={$rs_user['id']}");
		showmsg('登陆成功','/index.php?mod=user&ac=index');
	}
    showmsg('注册成功', WEB_PATH.'/index.php?mod=login');
}

$tpl -> display('default/'.$settings['templates'].'/register.html');
?>